<?php
/*  remoteFileReader 
    @version 0.9.1
 
    Copyright (C) 2011  Vespo Marco | http://thelairofthemad.altervista.org/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

-----------------------------------------------------------------------------------------------*/

//lettura file remoto
class remoteFileReader{
	
	protected $dataURL,$data,$someUA,$ua;
	
	function __construct($dataURL) {
		$this->dataURL = $dataURL;
		$this->someUA = array(		
			'Mozilla/5.0 (Windows; U; Windows NT 6.0; fr; rv:1.9.1b1) Gecko/20081007 Firefox/3.1b1',
			'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.0',
			'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13',
			'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)',
			'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.40607)',
			'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322)',
			'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.0.3705; Media Center PC 3.1; Alexa Toolbar; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
			'Mozilla/45.0 (compatible; MSIE 6.0; Windows NT 5.1)',
			'Mozilla/4.08 (compatible; MSIE 6.0; Windows NT 5.1)',
			'Mozilla/4.01 (compatible; MSIE 6.0; Windows NT 5.1)');
		$this->ua=$this->getRandomUserAgent();
	}
		
	protected function getRandomUserAgent() {
	    srand((double)microtime()*1000000);
	    return $this->someUA[rand(0,count($this->someUA)-1)];
	}
	
	public function getData(){
	
		if (function_exists('curl_init')) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $this->dataURL);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_USERAGENT,$this->ua);
			$this->data=curl_exec($ch);
			$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);			
			if ($this->data===false || $info != 200) 
      				return -1;
			return $this->data;			
		}
		else 
			return -2;		
	}		

	public function getDataUrl(){return $this->dataURL;}
	
	public function setDataUrl($url){$this->dataURL = $url;}
	
	public function setUserAgent($ua){$this->ua=$ua;}
}
?>
